<template>
  <el-form
    :show-message="false"
    :inline="config.inline"
    :label-width="config.labelWidth ? config.labelWidth + 'px' : '100px'"
    :model="formData"
    ref="form"
    class="lan-form-container"
  >
    <el-form-item
      v-for="item in config.formList"
      :key="item.name"
      :prop="item.name"
      :label="config.hideLabel ? '' : item.label"
      :required="item.required"
    >
      <el-tooltip
        :content="item.tooltip"
        v-if="item.tooltip"
        effect="light"
      >
        <i class="el-icon-question"></i>
      </el-tooltip>
      <div v-if="item.type === 'radio'">
        <el-radio-group
          v-model="formData[item.name]"
          v-if="!item.isButton"
          :size="item.size || config.size"
        >
          <el-radio
            v-for="(radio, index) in item.list"
            :key="index"
            :label="radio.label"
            :disabled="radio.disabled"
            :border="radio.border"
            :size="radio.size"
          >{{ radio.text }}</el-radio>
        </el-radio-group>
        <el-radio-group
          v-model="formData[item.name]"
          :text-color="item.textColor"
          :fill="item.fill"
          :size="item.size || config.size"
          v-else
        >
          <el-radio-button
            v-for="(radio, index) in item.list"
            :key="index"
            :label="radio.label"
            :disabled="radio.disabled"
            :size="radio.size"
          >{{ radio.text }}</el-radio-button>
        </el-radio-group>
      </div>
      <div v-if="item.type === 'checkbox'">
        <el-checkbox-group
          v-model="formData[item.name]"
          v-if="!item.isButton"
          :size="item.size || config.size"
          :min="item.min"
          :max="item.max"
        >
          <el-checkbox
            v-for="(radio, index) in item.list"
            :key="index"
            :label="radio.label"
            :disabled="radio.disabled"
            :border="radio.border"
            :size="radio.size"
          >{{ radio.text }}</el-checkbox>
        </el-checkbox-group>
        <el-checkbox-group
          v-model="formData[item.name]"
          :text-color="item.textColor"
          :fill="item.fill"
          :min="item.min"
          :max="item.max"
          :size="item.size || config.size"
          v-else
        >
          <el-checkbox-button
            v-for="(radio, index) in item.list"
            :key="index"
            :label="radio.label"
            :disabled="radio.disabled"
            :size="radio.size"
          >{{ radio.text }}</el-checkbox-button>
        </el-checkbox-group>
      </div>
      <el-select
        v-if="item.type === 'select'"
        :placeholder="item.placeholder"
        v-model="formData[item.name]"
        :size="item.size || config.size"
        :multiple="item.multiple"
        :disabled="item.disabled"
        :value-key="item.valueKey"
        :collapse-tags="item.collapseTags"
        :multiple-limit="item.multipleLimit"
        :style="{ width: item.width + 'px' }"
        clearable
      >
        <el-option
          v-for="option in typeof item.options === 'function'
            ? item.options()
            : item.options"
          :key="item.optionValue ? option[item.optionValue] : option.value"
          :label="item.optionLabel ? option[item.optionLabel] : option.label"
          :value="item.optionValue ? option[item.optionValue] : option.value"
        ></el-option>
      </el-select>
      <el-select
        :multiple="item.multiple"
        v-if="item.type === 'remoteSelect'"
        v-model="formData[item.name]"
        remote
        filterable
        :remote-method="item.query"
        :loading="config.remoteLoading"
        :placeholder="item.placeholder"
        :size="item.size || config.size"
        :style="{ width: item.width + 'px' }"
        @focus="item.query()"
        :disabled="item.disabled"
        :value-key="item.valueKey"
        :collapse-tags="item.collapseTags"
        :multiple-limit="item.multipleLimit"
        clearable
      >
        <el-option
          v-for="option in typeof item.options === 'function'
            ? item.options()
            : item.options"
          :key="item.optionValue ? option[item.optionValue] : option.value"
          :label="item.optionLabel ? option[item.optionLabel] : option.label"
          :value="item.optionValue ? option[item.optionValue] : option.value"
        ></el-option>
      </el-select>
      <el-input
        v-if="item.type === 'input'"
        clearable
        :show-password="item.isPassword"
        :placeholder="item.placeholder"
        v-model="formData[item.name]"
        :size="item.size || config.size"
        :style="{ width: item.width + 'px' }"
        :disabled="item.disabled"
        :readonly="item.readonly"
      ></el-input>
      <el-input
        v-if="item.type === 'textarea'"
        clearable
        type="textarea"
        :placeholder="item.placeholder"
        v-model="formData[item.name]"
        :style="{ width: item.width + 'px'}"
        :disabled="item.disabled"
        :readonly="item.readonly"
        :rows="item.rows"
        :autosize="item.autosize"
        class="xxx"
      ></el-input>
      <el-cascader
        v-if="item.type === 'cascader'"
        v-model="formData[item.name]"
        :placeholder="item.placeholder"
        :options="item.options"
        :disabled="item.disabled"
        :show-all-levels="showAllLevels"
        :collapse-tags="item.collapseTags"
        :size="item.size || config.size"
        :separator="item.separator"
        clearable
        :props="{
          expandTrigger: item.expandTrigger,
          multiple: item.multiple,
          checkStrictly: item.checkStrictly,
          value: item.optionValue || 'value',
          label: item.optionLabel || 'label',
          children: item.optionChildren || 'children',
          disabled: item.optonDisabled || 'disabled',
          leaf: item.optionLeaf || 'leaf',
        }"
      ></el-cascader>
      <el-switch
        v-if="item.type === 'switch'"
        :disabled="item.disabled"
        :width="item.width"
        :active-color="item.activeColor"
        :inactive-color="item.inactiveColor"
        v-model="formData[item.name]"
      ></el-switch>
      <el-slider
        v-if="item.type === 'slider'"
        :min="item.min"
        :max="item.max"
        v-model="formData[item.name]"
        :style="{ width: item.width + 'px' }"
        :disabled="item.disabled"
        :step="item.step"
        :show-input="item.showInput"
        :input-size="item.inputSize"
        :show-stops="item.showStops"
        :range="item.range"
        :marks="item.marks"
      ></el-slider>
      <el-time-picker
        v-if="item.type === 'time'"
        v-model="formData[item.name]"
        :placeholder="item.placeholder"
        clearable
        :size="item.size"
        :arrow-control="item.arrowControl"
        :picker-options="item.pickerOptions"
        :default-value="item.defaultValue"
        :value-format="item.valueFormat"
        :disabled="item.disabled"
        :readonly="item.readonly"
      ></el-time-picker>
      <el-time-picker
        v-if="item.type === 'timeRange'"
        v-model="formData[item.name]"
        :start-placeholder="item.startPlaceholder"
        :end-placeholder="item.endPlaceholder"
        :is-range="true"
        clearable
        :size="item.size || config.size"
        :format="item.format"
        :picker-options="item.pickerOptions"
        :default-time="item.defaultTime"
        :value-format="item.valueFormat"
        :disabled="item.disabled"
        :readonly="item.readonly"
      ></el-time-picker>
      <el-date-picker
        v-if="item.type === 'date'"
        v-model="formData[item.name]"
        :type="item.timeType || 'date'"
        :placeholder="item.placeholder"
        :disabled="item.disabled"
        :readonly="item.readonly"
        :size="item.size || config.size"
        :format="item.format"
        :picker-options="item.pickerOptions"
        :default-value="item.defaultValue"
        :value-format="item.valueFormat"
        clearable
      ></el-date-picker>
      <el-date-picker
        v-if="item.type === 'dateRange'"
        v-model="formData[item.name]"
        :type="item.timeType + 'range' || 'daterange'"
        :start-placeholder="item.startPlaceholder"
        :end-placeholder="item.endPlaceholder"
        :disabled="item.disabled"
        :readonly="item.readonly"
        clearable
        :size="item.size || config.size"
        :format="item.format"
        :picker-options="item.pickerOptions"
        :default-time="item.defaultTime"
        :value-format="item.valueFormat"
      ></el-date-picker>
      <div v-if="item.type === 'button'">
        <el-button
          v-for="(button, index) in item.list"
          :key="index"
          :icon="button.icon.includes('el-icon') ? button.icon : ''"
          :class="[button.type ? 'buttons-' + button.type : '']"
          :type="button.type"
          :size="button.size || item.size"
          :plain="button.plain"
          :round="button.round"
          :circle="button.circle"
          :loading="button.isLoadingButton?config.btnLoading:false"
          @click="button.method"
        >
          <svg-icon
            v-if="button.icon && !button.icon.includes('el-icon')"
            :icon-class="button.icon"
            class="svg-icon"
          ></svg-icon>{{ button.text }}
        </el-button>
      </div>
      <!-- <el-date-picker
          v-model="formData[item.name[0]]"
          :type="item.timeType || 'date'"
          v-if="item.type === 'timerange'"
          :placeholder="item.startPlaceholder || '请选择开始时间'"
          clearable
        ></el-date-picker>
        <span v-if="item.type === 'timerange'">&nbsp;至&nbsp;</span>
        <el-date-picker
          v-model="formData[item.name[1]]"
          :type="item.timeType || 'date'"
          v-if="item.type === 'timerange'"
          :placeholder="item.endPlaceholder || '请选择结束时间'"
          clearable
        ></el-date-picker> -->
      <slot
        v-if="item.type === 'slot'"
        :name="item.name"
        :data="formData"
      ></slot>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  props: {
    config: {
      type: Object,
      default: () => {}
    },
    formData: {
      type: Object,
      default: () => {}
    }
  },
  methods: {
    resetFields() {
      this.$refs.form.resetFields();
    }
  }
};
</script>

<style lang="scss" scoped>
.lan-form-container {
  .el-tooltip {
    margin-right: 10px;
  }
  .el-form-item {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
  }

  /deep/.el-form-item__label {
    height: inherit;
    display: block;
  }
  /deep/.el-form-item__content {
    margin-left: 0;
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-left: 0 !important;
  }
}
</style>
